package com.qiaofang.collectmanage.dao.impl;
import com.qiaofang.collectmanage.dao.CrawlerSubTaskDao;
import com.qiaofang.collectmanage.dao.domain.CrawlerSubTask;
import com.qiaofang.collectmanage.dao.domain.CrawlerSubTask;
import com.qiaofang.collectmanage.dao.mapper.CrawlerSubTaskMapper;
import com.qiaofang.collectmanage.dao.CrawlerSubTaskDao;
import com.qiaofang.collectmanage.stub.constants.DeletedEnum;
import com.qiaofang.common.model.page.PageDTO;
import com.qiaofang.common.page.PageUtil;
import com.qiaofang.collectmanage.stub.bean.SearchCrawlerSubTaskDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
import tk.mybatis.mapper.entity.Example;
import org.apache.ibatis.session.RowBounds;

/**
* Created by generator on 2018-8-1 14:39:02
*/
@Repository("crawlerSubTaskDao")
public class CrawlerSubTaskDaoImpl implements CrawlerSubTaskDao {

    @Autowired
    private CrawlerSubTaskMapper subTaskMapper;

    @Override
    public CrawlerSubTask  getCrawlerSubTaskByUuid(String uuid){
       CrawlerSubTask  subTask = new CrawlerSubTask();
       subTask.setUuid(uuid);
        return subTaskMapper.selectOne(subTask);
   }


    @Override
    public CrawlerSubTask getCrawlerSubTaskByCrawlerSubTask(CrawlerSubTask subTask){
        return subTaskMapper.selectOne(subTask);
    }

    @Override
    public int saveCrawlerSubTask(CrawlerSubTask subTask) {
         return subTaskMapper.insertSelective(subTask);
    }

    @Override
    public int  updateCrawlerSubTask (CrawlerSubTask subTask) {
        Example ex = new Example(CrawlerSubTask.class);
         ex.createCriteria()
         .andIsNotNull("uuid")
         .andEqualTo("uuid",subTask.getUuid());
        return  subTaskMapper.updateByExampleSelective(subTask,ex);
    }

    @Override
    public int removeCrawlerSubTaskByUuid (String uuid){
        Example ex = new Example(CrawlerSubTask.class);
        ex.createCriteria().andEqualTo("uuid",uuid);
        CrawlerSubTask subTask = new  CrawlerSubTask();
        subTask.setDeleted(DeletedEnum.YES.getValue());
        return  subTaskMapper.updateByExampleSelective(subTask,ex);
    }


    @Override
    public List<CrawlerSubTask> listCrawlerSubTask(CrawlerSubTask subTask){
          if(subTask == null){
              subTask  = new CrawlerSubTask();
          }
         subTask.setDeleted(DeletedEnum.NO.getValue());
          return subTaskMapper.select(subTask);
    }


        @Override
        public List<CrawlerSubTask> listCrawlerSubTaskByUuids(List<String> uuidList){
           Example ex = new Example(CrawlerSubTask.class);
           ex.createCriteria().andIn("crawlerTaskUuid",uuidList);
           return subTaskMapper.selectByExample(ex);
        }

    @Override
    public  Long countCrawlerSubTask(CrawlerSubTask subTask){
        if(subTask == null){
          subTask  = new CrawlerSubTask();
        }
         subTask.setDeleted(DeletedEnum.NO.getValue());
         return Long.valueOf(subTaskMapper.selectCount(subTask));
    }

    @Override
    public List<CrawlerSubTask> searchCrawlerSubTaskBySimpleAttr(CrawlerSubTask subTask,RowBounds rb){
        if(subTask == null){
           subTask  = new CrawlerSubTask();
        }
         subTask.setDeleted(DeletedEnum.NO.getValue());
         return subTaskMapper.selectByRowBounds(subTask,rb);
    }

    @Override
    public List<CrawlerSubTask> searchCrawlerSubTaskByPermission(SearchCrawlerSubTaskDTO dto,PageDTO page){
        Example example = new Example(CrawlerSubTask.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deleted", 0);
        return subTaskMapper.selectByExampleAndRowBounds(example,PageUtil.getRowBounds(page));
    }

    @Override
    public Long countCrawlerSubTaskByPermission(SearchCrawlerSubTaskDTO dto){
        Example example = new Example(CrawlerSubTask.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deleted", 0);
       return Long.valueOf(subTaskMapper.selectCountByExample(example));
    }
}
